package vn.edu.uit.chemistry.utilities;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.net.Socket;

import vn.edu.uit.chemistry.NhanFile;
import vn.edu.uit.chemistry.protocol.BaiCu;
import vn.edu.uit.chemistry.protocol.BaiGiang;
import vn.edu.uit.chemistry.protocol.BatKy;
import vn.edu.uit.chemistry.protocol.CapNhatKiemTra15;
import vn.edu.uit.chemistry.protocol.DapAnKiemTra15;
import vn.edu.uit.chemistry.protocol.KetQuaKiemTra15;
import vn.edu.uit.chemistry.protocol.KiemTra15;
import vn.edu.uit.chemistry.protocol.KiemTra45;
import vn.edu.uit.chemistry.protocol.Message;
import vn.edu.uit.chemistry.protocol.TaiLieuThamKhao;
import vn.edu.uit.chemistry.protocol.ThongTinCaNhan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class ClientThreadCommand implements Runnable {

	private Socket socket;
	private Thread thread;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	Context ctx;

	public ClientThreadCommand(Socket sk, Context _ctx) throws IOException {
		System.out.println("Client connected Command");
		this.socket = sk;
		this.ctx = _ctx;
	}

	public void start() throws IOException {

		oos = new ObjectOutputStream(socket.getOutputStream());
		ois = new ObjectInputStream(socket.getInputStream());

		thread = new Thread(this);
		thread.start();
		while (!thread.isAlive()) {
			//
		}

	}

	public void stop() throws IOException {

		if (socket != null) {
			socket.shutdownInput();
			socket.shutdownOutput();
			socket.close();
		}
	}

	public void run() {
		try {
			while (true) {
				handleMsg((Message) ois.readObject());
			}
		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void handleMsg(Message msg) throws IOException {
		if (msg instanceof KiemTra15) {
			nhanFileKT15((KiemTra15) msg);
		} else if (msg instanceof KiemTra45) {
			nhanFileKT45((KiemTra45) msg);
		} else if (msg instanceof CapNhatKiemTra15) {
			capNhatKT15((CapNhatKiemTra15) msg);
		} else if (msg instanceof KetQuaKiemTra15) {
			nhanKetQuaKT15((KetQuaKiemTra15) msg);
		} else if (msg instanceof DapAnKiemTra15) {
			nhanDapAnKT15((DapAnKiemTra15) msg);
		} else if (msg instanceof BaiGiang) {
			nhanTaiLieuGV((BaiGiang) msg);
		} else if (msg instanceof BaiCu) {
			nhanFileBaiCu((BaiCu) msg);
		} else if (msg instanceof BatKy) {
			nhanFileBatKy((BatKy) msg);
		} else if (msg instanceof TaiLieuThamKhao) {
			nhanFileTaiLieu((TaiLieuThamKhao) msg);
		} else if (msg instanceof ThongTinCaNhan) {
			oos.writeObject(new ThongTinCaNhan("", "", "", ""));

			String tenBB = ((ThongTinCaNhan) msg).ten;
			String mscn = ((ThongTinCaNhan) msg).mshs;
			String host = ((ThongTinCaNhan) msg).host;

			SharedPreferences prefs = ctx.getSharedPreferences("DSBanBe", 0); 
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(mscn, tenBB + "#" + host);
			editor.commit();
		}
	}

	private void nhanFileTaiLieu(TaiLieuThamKhao msg) throws IOException {
		String nameFile = ((TaiLieuThamKhao) msg).nameFile;
		String sizeFile = ((TaiLieuThamKhao) msg).sizeFile;
		String sender = ((TaiLieuThamKhao) msg).sender;
		String ms = ((TaiLieuThamKhao) msg).mshs;
		String host = ((TaiLieuThamKhao) msg).host;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		if (ms.substring(0, 2).equals("gv")) {
			editor.putString("kind", "TLTGV"); // nhan file bat ky tu giao vien
		} else if (ms.substring(0, 2).equals("hs")) {
			editor.putString("kind", "TLTBB"); // nhan file bat ky tu ban be
		}

		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new BatKy("", "", "", "", ""));

		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);
	}

	private void nhanFileBaiCu(BaiCu msg) throws IOException {
		String nameFile = ((BaiCu) msg).nameFile;
		String sizeFile = ((BaiCu) msg).sizeFile;
		String sender = ((BaiCu) msg).sender;
		String ms = ((BaiCu) msg).mshs;
		String host = ((BaiCu) msg).host;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		if (ms.substring(0, 2).equals("gv")) {
			editor.putString("kind", "DABCGV"); // dap an bai cu tu giao vien
		} else if (ms.substring(0, 2).equals("hs")) {
			editor.putString("kind", "DABCBB"); // dap an bai cu tu ban be
		}

		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new BaiCu("", "", "", "", ""));

		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);
	}

	private void nhanTaiLieuGV(BaiGiang msg) throws IOException {

		String nameFile = ((BaiGiang) msg).nameFile;
		String sizeFile = ((BaiGiang) msg).sizeFile;
		String sender = ((BaiGiang) msg).sender;
		String ms = ((BaiGiang) msg).mshs;
		String host = ((BaiGiang) msg).host;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		if (ms.substring(0, 2).equals("gv")) {
			editor.putString("kind", "TLGV"); // nhan tai lieu tu giao vien
		} else if (ms.substring(0, 2).equals("hs")) {
			editor.putString("kind", "TLBB"); // nhan tai lieu tu ban be
		}

		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new BaiGiang("", "", "", "", ""));

		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);
	}

	private void nhanFileBatKy(BatKy msg) throws IOException {
		String nameFile = ((BatKy) msg).nameFile;
		String sizeFile = ((BatKy) msg).sizeFile;
		String sender = ((BatKy) msg).sender;
		String ms = ((BatKy) msg).mshs;
		String host = ((BatKy) msg).host;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		if (ms.substring(0, 2).equals("gv")) {
			editor.putString("kind", "BKGV"); // nhan file bat ky tu giao vien
		} else if (ms.substring(0, 2).equals("hs")) {
			editor.putString("kind", "BKBB"); // nhan file bat ky tu ban be
		}

		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new BatKy("", "", "", "", ""));

		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);
	}

	private void nhanFileKT15(KiemTra15 msg) throws IOException { // nhan file
																	// kiem tra
																	// 15 phut
		String nameFile = ((KiemTra15) msg).nameFile;
		String sizeFile = ((KiemTra15) msg).sizeFile;
		String mscn = ((KiemTra15) msg).mshs;
		String host = ((KiemTra15) msg).host;
		String sender = ((KiemTra15) msg).sender;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("kind", "BKT15");
		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new KiemTra15(nameFile, sizeFile, sender, mscn, host));

		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);
	}

	private void capNhatKT15(CapNhatKiemTra15 msg) {
		SharedPreferences prefs = ctx.getSharedPreferences("LAMBAI", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt("start", 1);
		editor.commit();
	}

	private void nhanDapAnKT15(DapAnKiemTra15 msg) throws IOException { // loi
																		// giai
																		// chi
																		// tiet
		String nameFile = ((DapAnKiemTra15) msg).nameFile;
		String sizeFile = ((DapAnKiemTra15) msg).sizeFile;
		String host = ((DapAnKiemTra15) msg).host;
		String sender = ((DapAnKiemTra15) msg).sender;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("kind", "DAKT15");
		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new DapAnKiemTra15("", "", "", "", ""));
	}

	private void nhanFileKT45(KiemTra45 msg) throws IOException {
		String nameFile = ((KiemTra45) msg).nameFile;
		String sizeFile = ((KiemTra45) msg).sizeFile;
		String host = ((KiemTra45) msg).host;
		String sender = ((KiemTra45) msg).sender;

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("kind", "KT45");
		editor.putString("name", nameFile);
		editor.putString("size", sizeFile);
		editor.putString("sender", sender);
		editor.putInt("percent", 0);
		editor.putString("host", host);
		editor.commit();

		oos.writeObject(new KiemTra45(nameFile, sizeFile, sender, host));
		Intent intent = new Intent(ctx, NhanFile.class);
		ctx.startActivity(intent);

	}

	private void nhanKetQuaKT15(KetQuaKiemTra15 msg) {
		String gvDapAn = ((KetQuaKiemTra15) msg).gvDapAn.toUpperCase();

		SharedPreferences prefs = ctx.getSharedPreferences("ChamDiem", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("dapanGV", gvDapAn);
		editor.commit();
	}
}
